package org.mindrive.system.service.system;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;
import org.mindrive.system.collection.system.PermissionEntity;
import org.mindrive.system.collection.system.RoleEntity;
import org.mindrive.system.collection.system.RolePermissionEntity;
import org.mindrive.system.dao.PermissionDao;
import org.mindrive.system.dao.RoleDao;
import org.mindrive.system.dao.RolePermissionDao;
import org.mindrive.system.service.BaseConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PermissionTemplateReaderHandler extends AnalysisEventListener<Map<Integer, String>> {

    @Autowired
    public PermissionDao permissionDao;

    @Autowired
    public RoleDao roleDao;

    @Autowired
    public RolePermissionDao rolePermissionDao;


    @Value("${three.member.management.enabled}")
    public boolean threeMemeberManagementEnabled;

    List<Map<Integer, String>> list = new ArrayList<Map<Integer, String>>();

    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
        if (data.get(0) != null && data.get(1) != null && data.get(2) != null) {
            list.add(data);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.debug("初期化权限开始");
        int order = 100;

        for (int i = 0; i < list.size(); i++) {

            Map<Integer, String> data = list.get(i);
            String id = data.get(0);
            String title = data.get(1);
            String menuType = data.get(2);
            String route = data.get(4);
            String icon = data.get(5);
            String parentId = data.get(6);
            String permissionCode = data.get(7);
            String roleCodes = data.get(8);
            String systemCode = data.get(9);
            log.debug("第[{}]条数据:[{}]", i, title);

            PermissionEntity permission = new PermissionEntity();
            permission.setId(id);
            permission.setMenuType(menuType);
            permission.setTitle(title);
            permission.setSortNo(order);
            permission.setIcon(icon);
            permission.setRoute(route);
            permission.setRoleCode(roleCodes);
            permission.setPermissionCode(permissionCode != null ? permissionCode : "");
            permission.setParentId(parentId == null ? "" : parentId);
            if (systemCode.equals("system")) {
                permission.setSystemCode("");
            } else {
                permission.setSystemCode(systemCode);
            }
            permissionDao.save(permission);

            if (parentId != null && !"".equals(parentId)) {
                permissionDao.createPath(parentId, id);
            }

            order += 100;

            if (permission.getRoleCode().contains(BaseConstant.ROLE_CODE_SYSTEM_ADMIN)) {

                RoleEntity role = roleDao.getRoleByCode("", BaseConstant.ROLE_CODE_SYSTEM_ADMIN);
                RolePermissionEntity rolePermission = new RolePermissionEntity();
                rolePermission.setPermissionId(permission.getId());
                rolePermission.setRoleId(role.getId());
                rolePermission.setCreatedAt(new Date());
                rolePermissionDao.save(rolePermission);
            }


            if (permission.getRoleCode().contains(BaseConstant.ROLE_CODE_SYSTEM_USER)) {

                RoleEntity role = roleDao.getRoleByCode("", BaseConstant.ROLE_CODE_SYSTEM_USER);
                RolePermissionEntity rolePermission = new RolePermissionEntity();
                rolePermission.setPermissionId(permission.getId());
                rolePermission.setRoleId(role.getId());
                rolePermission.setCreatedAt(new Date());
                rolePermissionDao.save(rolePermission);

            }


        }

    }

}
